<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="utf-8">
    <title>编辑课程计划</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../../static/layuimini/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../../../static/layuimini/css/public.css" media="all">
    <style>
        .layui-form-item .layui-input-company {
            width: auto;
            padding-right: 10px;
            line-height: 38px;
        }
    </style>

    <script src="../../../static/layuimini/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
    <script src="../../../static/layuimini/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
    <script src1="http://local.res.layui.com/layui/release/laydate/dist/laydate.js" charset="utf-8"></script>

    <script type="text/javascript" charset="UTF-8">
        //获取url里，以name为名字的参数内容
        function getQueryString(name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
            var r = decodeURI(window.location.search.substr(1).match(reg));
            var arr = r.split(",");
            if (r != null) return arr[2];
            return null;
        }
    </script>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <div class="layui-form layuimini-form" lay-filter="first">
            <div class="layui-form-item">
                <label class="layui-form-label required">课程计划名</label>
                <div class="layui-input-block">
                    <input type="text" name="curriculumPlanName" lay-verify="required" lay-reqtext="课程计划名不能为空"
                           placeholder="请输入课程计划名"
                           value="" class="layui-input" id="curriculumPlanName">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">学年</label>
                <div class="layui-input-block">
                    <select name="schoolYear" data-value="" lay-verify="required" lay-verType="tips"
                            lay-filter="schoolYear" id="schoolYear">
                        <option value="">请选择学年</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">学期</label>
                <div class="layui-input-block">
                    <select name="semester" data-value="" lay-verify="required" lay-verType="tips"
                            lay-filter="semester" id="semester">
                        <option value="">请选择学期</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">系别</label>
                <div class="layui-input-block">
                    <select name="department" data-value="" lay-verify="required" lay-verType="tips"
                            lay-filter="department" id="department">
                        <option value="">请选择系别</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">专业</label>
                <div class="layui-input-block">
                    <select name="major" data-value="" lay-verify="required" lay-verType="tips" lay-filter="major"
                            id="major">
                        <option value="">请选择专业</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">课程</label>
                <div class="layui-input-block">
                    <input type="text" name="curriculumList" placeholder="请选择课程" autocomplete="off" class="layui-input"
                           id="curriculumList" ts-selected="" value="">
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">保存</button>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    layui.use(['form', 'miniTab', 'jquery', 'table', 'tableSelect'], function () {
        var form = layui.form,
            layer = layui.layer,
            miniTab = layui.miniTab,
            $ = layui.jquery,
            table = layui.table,
            tableSelect = layui.tableSelect;

        var curriculumPlanName, schoolYear, semester, department, major, curriculumId, curriculumName;
        //获取回填数据
        $.ajax({
            async: false,
            url: "/getCurriculumPlanById",
            type: "get",
            data: {
                curriculumPlanId: getQueryString("id")
            },
            datatype: "json",
            success: function (data) {
                console.log(data);
                curriculumPlanName = data.curriculumPlanName;
                schoolYear = data.schoolYear;
                semester = data.semester;
                department = data.department;
                major = data.major;
                curriculumId = data.curriculumId;
                curriculumName = data.curriculumName;
            }
        });

        //初始化学年
        $.ajax({
            async: false,
            url: "/getSchoolYear",
            type: "get",
            datatype: "json",
            success: function (data) {
                $.each(data, function (index, element) {
                    $("#schoolYear").append("<option value='" + element.value + "'>" + element.text + "</option>");

                });
                layui.form.render('select');
            }
        });
        //初始化系别
        $.ajax({
            async: false,
            url: "/getDepartment",
            type: "get",
            datatype: "json",
            success: function (data) {
                $.each(data, function (index, element) {
                    $("#department").append("<option value='" + element.departmentName + "'>" + element.departmentName + "</option>");
                });
                layui.form.render('select');
            }
        });
        //初始化学期
        $.ajax({
            async: false,
            url: "/getSemester",
            type: "get",
            datatype: "json",
            success: function (data) {
                $("#semester").empty();
                $("#semester").append("<option value=''>学期</option>");
                $.each(data, function (index, element) {
                    $("#semester").append("<option value='" + element + "'>" + element + "</option>");
                });
                layui.form.render('select');
            }
        });
        //初始化专业
        $.ajax({
            async: false,
            url: "/getMajorByDepartment",
            type: "get",
            data: {
                departmentName: department
            },
            datatype: "json",
            success: function (data) {
                $("#major").empty();
                $("#major").append(" <option value=''>请选择专业</option>");
                $.each(data, function (index, element) {
                    $("#major").append("<option value='" + element.majorName + "'>" + element.majorName + "</option>");
                });
                layui.form.render('select');
            }
        });
        //监听：级联查询学期
        form.on('select(schoolYear)', function (data) {
            console.log('select.schoolYear：', this, data);
            $.get("/getSemester", function (data) {
                $("#semester").empty();
                $("#semester").append("<option value=''>学期</option>");
                $.each(data, function (index, element) {
                    $("#semester").append("<option value='" + element + "'>" + element + "</option>");
                });
                layui.form.render('select');
            }, "json");
        });
        //监听：级联查询专业
        form.on('select(department)', function (data) {
            console.log('select.department：', this, data);
            let departmentName = $("#department>option:selected").val();
            $.get("/getMajorByDepartment", {departmentName: departmentName}, function (data) {
                $("#major").empty();
                $("#major").append(" <option value=''>请选择专业</option>");
                $.each(data, function (index, element) {
                    $("#major").append("<option value='" + element.majorName + "'>" + element.majorName + "</option>");
                });
                layui.form.render('select');
            }, "json");
        });

        form.on('select(semester)', function (data) {
            console.log('select.semester：', this, data);
        });
        form.on('select(major)', function (data) {
            console.log('select.major：', this, data);
        });

        //初始赋值
        var thisValue = form.val('first', {
            'curriculumPlanName': curriculumPlanName
            , 'schoolYear': schoolYear
            , 'semester': semester
            , 'department': department
            , 'major': major
            , 'curriculumList': curriculumName
        });
        $("#curriculumList").attr("ts-selected", curriculumId);//初始赋值属性

        console.log("curriculumPlanName" + curriculumPlanName);


        var curriculum;//课程id临时变量
        tableSelect.render({
            elem: '#curriculumList',
            searchKey: 'curriculumName',
            checkedKey: 'curriculumId',
            searchPlaceholder: '输入课程名进行查找',
            table: {
                url: '/getCurriculumList',
                cols: [[
                    {type: 'checkbox'},
                    {field: 'curriculumId', title: '课程号', width: 100},
                    {field: 'curriculumName', title: '课程名', width: 300},
                    {field: 'teacherName', title: '教师名', width: 100}
                ]]
            },
            done: function (elem, data) {
                var NEWJSONID = []
                var NEWJSON = []
                layui.each(data.data, function (index, item) {
                    NEWJSONID.push(item.curriculumId)
                    NEWJSON.push(item.curriculumName)
                });
                elem.val(NEWJSONID.join(","));
                elem.val(NEWJSON.join(","));
                console.log(NEWJSONID);
                curriculum = NEWJSONID.toString();//讲数组转成字符串
            }
        })

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            let curriculumPlanName = $("#curriculumPlanName").val(),
                schoolYear = $("#schoolYear").val(),
                semester = $("#semester").val(),
                department = $("#department").val(),
                major = $("#major").val()
            curriculumName = $("#curriculumList").val();
            $.ajax({
                url: "/editCurriculumPlan",
                type: "get",
                data: {
                    id: getQueryString("id")
                    , curriculumPlanName: curriculumPlanName
                    , schoolYear: schoolYear
                    , semester: semester
                    , department: department
                    , major: major
                    , curriculumId: $("#curriculumList").attr("ts-selected")
                    , curriculumName: curriculumName
                },
                datatype: "text",
                success: function (data) {
                    if (data == "success") {
                        layer.msg('课程计划编辑成功', {
                            icon: 1,
                            time: 1000, //1秒关闭（如果不配置，默认是3秒）
                        }, function () {
                            // parent.layer.closeAll('iframe');
                            let index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                            parent.layer.close(index); //再执行关闭
                            parent.window.location.reload();//刷新页面
                        });
                    } else {
                        layer.msg('课程计划编辑失败，请稍后再试！', {
                            icon: 0,
                            time: 1000, //1秒关闭（如果不配置，默认是3秒）
                        }, function () {
                            // parent.layer.closeAll('iframe');
                            let index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                            parent.layer.close(index); //再执行关闭
                        });
                    }
                }
            });
        });


    });
</script>
</body>
</html>